package com.r2coding.system.controller;

import com.r2coding.common.enums.Module;
import com.r2coding.common.enums.OperationType;
import com.r2coding.common.util.PageUtils;
import com.r2coding.common.util.Result;
import com.r2coding.system.annotation.SysLog;
import com.r2coding.system.service.RoleService;
import com.r2coding.system.vo.req.QueryUserVo;
import com.r2coding.system.vo.req.UserLockVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.r2coding.system.entity.UserEntity;
import com.r2coding.system.service.UserService;

import java.util.List;

/**
 * 用户信息控制器
 *
 * @author 98k灬
 * @email hk666zx@163.com
 * @date 2022-04-04 10:30:49
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    RoleService roleService;

    /**
     * 列表
     */
    @PostMapping("/list")
    public Result list(@RequestBody QueryUserVo vo) {
        PageUtils page = userService.queryAll(vo);
        return Result.ok(page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{userId}")
    public Result info(@PathVariable("userId") Integer userId) {
        UserEntity user = userService.getUserById(userId);
        return Result.ok(user);
    }

    /**
     * 保存
     */
    @SysLog(module = Module.SYSTEM, operationType = OperationType.INSERT, description = "保存用户")
    @PreAuthorize("hasAuthority('user:add')")
    @RequestMapping("/save")
    public Result save(@RequestBody UserEntity user){
		userService.save(user);
        return Result.ok();
    }

    /**
     * 修改
     */
    @SysLog(module = Module.SYSTEM, operationType = OperationType.UPDATE, description = "修改用户")
    @PreAuthorize("hasAuthority('user:edit')")
    @RequestMapping("/update")
    public Result update(@RequestBody UserEntity user){
		userService.updateById(user);
        return Result.ok();
    }

    /**
     * 删除
     */
    @SysLog(module = Module.SYSTEM, operationType = OperationType.DELETE, description = "删除用户")
    @PreAuthorize("hasAuthority('user:delete')")
    @DeleteMapping("/delete/{userId}")
    public Result delete(@PathVariable("userId") Integer userId) {
        userService.deleteUser(userId);
        return Result.ok();
    }

    /**
     * 根据角色id查询用户信息
     *
     * @param userId 角色id
     * @return 用户信息
     */
    @GetMapping("/getUserRoleIds/{userId}")
    public Result getUserRoleIds(@PathVariable("userId") Integer userId) {
        List<Integer> ids = userService.getUserRoleIdsByUserId(userId);
        return Result.ok(ids);
    }

    /**
     * 锁定用户
     *
     * @param vo 请求体
     * @return 统一响应体
     */
    @PreAuthorize("hasAuthority('user:edit')")
    @PostMapping("/lock")
    public Result lock(@RequestBody UserLockVo vo){
        log.info("vo: {}",vo.toString() );
        userService.lock(vo);
        return Result.ok();
    }
}
